package com.ruoyi.bean.controller;

import com.ruoyi.bean.service.TenantService;
import com.ruoyi.common.result.Result;
import com.ruoyi.common.util.StringUtils;
import com.ruoyi.domain.bo.SysTenantBo;
import com.ruoyi.domain.vo.LoginTenantVo;
import com.ruoyi.domain.vo.SysTenantVo;
import com.ruoyi.domain.vo.TenantListVo;
import io.github.stylesmile.annotation.AutoWired;
import io.github.stylesmile.annotation.Controller;
import io.github.stylesmile.annotation.RequestMapping;
import io.github.stylesmile.server.Request;
import io.github.stylesmile.tool.CollectionUtil;

import java.net.URL;
import java.util.List;

@Controller
public class AuthController {
    @AutoWired
    TenantService tenantService;

    /**
     * 登录页面租户下拉框
     * @param request
     * @return Result
     * @throws Exception Exception
     */
    @RequestMapping("/auth/tenant/list")
    public Result<LoginTenantVo> tenantList(Request request) throws Exception {
        List<SysTenantVo> tenantList = tenantService.queryList(new SysTenantBo());
//        List<TenantListVo> voList = MapstructUtils.convert(tenantList, TenantListVo.class);
        // 获取域名
        String host;
        String referer = request.getHeaders().get("referer");
//        if (StringUtils.isNotBlank(referer)) {
//            // 这里从referer中取值是为了本地使用hosts添加虚拟域名，方便本地环境调试
//            host = referer.split("//")[1].split("/")[0];
//        } else {
//            host = new URL(request.getRequestURL().toString()).getHost();
//        }
        // 根据域名进行筛选
//        List<TenantListVo> list = StreamUtils.filter(voList, vo ->
//                StringUtils.equals(vo.getDomain(), host));
        // 返回对象
        LoginTenantVo vo = new LoginTenantVo();
//        vo.setVoList(CollectionUtil.isNotEmpty(list) ? list : voList);
        vo.setVoList(tenantList);
//        vo.setTenantEnabled(TenantHelper.isEnable());
        return Result.ok(vo);
    }
}
